package com.wteam.Service.impl;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.wteam.PoJo.DO.Comment;
import com.wteam.PoJo.DO.Likes;
import com.wteam.PoJo.DO.Postwork;
import com.wteam.PoJo.DO.ReplyComment;
import com.wteam.PoJo.VO.ReplyCommentVO;
import com.wteam.mapper.LikesMapper;
import com.wteam.Service.ILikesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wteam.until.Result;
import com.wteam.until.UserContext;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 点赞记录表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-16
 */
@Service
public class LikesServiceImpl extends ServiceImpl<LikesMapper, Likes> implements ILikesService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public Result likepostwork(Long id) {
        Likes likes = new Likes();
        likes.setUserId(UserContext.getUser());
        likes.setType(1);
        likes.setParentId(id);
        save(likes);

        Db.lambdaUpdate(Postwork.class)
                .eq(Postwork::getId, id)
                .setSql("likes =likes + 1")
                .update();
        redisTemplate.delete("postwork::"+ id.toString());
        return Result.success("点赞成功");

    }

    @Override
    public Result cannlelikepost(Long id) {

        lambdaUpdate().eq(Likes::getUserId, UserContext.getUser())
                .eq(Likes::getParentId, id)
                .remove();

        Db.lambdaUpdate(Postwork.class)
                .eq(Postwork::getId, id)
                .setSql("likes =likes - 1");
        redisTemplate.delete("postwork::"+ id.toString());
        return Result.success("取消成功");
    }

    @Override
    public Result likecomment(Long id, Integer type) {
        if (type==1){
        Db.lambdaUpdate(Comment.class)
                .eq(Comment::getId,id)
                .setSql("likes = likes +1")
                .update();

        Likes likes=new Likes();
        likes.setType(3);
        likes.setCommentId(id);
        likes.setUserId(UserContext.getUser());
        save(likes);
        return  Result.success("点赞成功");
        }
        if (type==2){
            Db.lambdaUpdate(ReplyComment.class)
                    .eq(ReplyComment::getId,id)
                    .setSql("likes = likes +1")
                    .update();

            Likes likes=new Likes();
            likes.setType(4);
            likes.setReplyId(id);
            likes.setUserId(UserContext.getUser());
            save(likes);
            return  Result.success("点赞成功");
        }
        return Result.error("类型错误");
    }

    @Override
    public Result cannlelikecomment(Long id, Integer type) {

        if (type==1){
            Db.lambdaUpdate(Comment.class)
                    .eq(Comment::getId,id)
                    .setSql("likes = likes - 1")
                    .update();
            lambdaUpdate()
                    .eq(Likes::getType,3)
                    .eq(Likes::getCommentId,id)
                    .eq(Likes::getUserId,UserContext.getUser())
                    .remove();

            return  Result.success("取消成功");
        }
        if (type==2){
            Db.lambdaUpdate(ReplyComment.class)
                    .eq(ReplyComment::getId,id)
                    .setSql("likes = likes -1")
                    .update();

            lambdaUpdate()
                    .eq(Likes::getType,4)
                    .eq(Likes::getReplyId,id)
                    .eq(Likes::getUserId,UserContext.getUser())
                    .remove();
            return  Result.success("取消成功");
        }
        return Result.error("类型错误");
    }
}
